package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.domain.Heart;
import com.ruoyi.system.domain.vo.HeartVO;
import com.ruoyi.system.mapper.DeviceMapper;
import com.ruoyi.system.service.IHeartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 心跳Controller
 *
 * @author wxd
 * @date 2024-08-29
 */
@Api(value = "心跳", tags = {"心跳"})
@RestController
@RequestMapping("/system/heart")
public class HeartController extends BaseController {
    @Autowired
    private IHeartService heartService;
    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 获取心跳详细信息
     */
    @ApiOperation("心跳检测")
    @Anonymous
    @GetMapping(value = "/device/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        Device device = deviceMapper.selectDeviceById(id);
        if (device == null) {
            return error("心跳检测失败");
        }
        Heart heart = new Heart();
        heart.setDeviceId(device.getId());
        heart.setHeartTime(new Date());
        heartService.insertHeart(heart);
        HeartVO heartVO = new HeartVO();
        heartVO.setDeviceId(device.getId());
        heartVO.setEmpowerType(device.getEmpowerType());
        heartVO.setEmpowerStatus(device.getEmpowerStatus());
        heartVO.setNowTime(new Date());
        device.setStatus("在线");
        device.setLastOnlineTime(new Date());
        deviceMapper.updateDevice(device);
        return success(heartVO);
    }

    /**
     * 查询心跳列表
     */
    @PreAuthorize("@ss.hasPermi('system:heart:list')")
    @GetMapping("/list")
    public TableDataInfo list(Heart heart) {
        startPage();
        List<Heart> list = heartService.selectHeartList(heart);
        return getDataTable(list);
    }

    /**
     * 导出心跳列表
     */
    @PreAuthorize("@ss.hasPermi('system:heart:export')")
    @Log(title = "心跳", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Heart heart) {
        List<Heart> list = heartService.selectHeartList(heart);
        ExcelUtil<Heart> util = new ExcelUtil<Heart>(Heart.class);
        util.exportExcel(response, list, "心跳数据");
    }

    /**
     * 获取心跳详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:heart:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(heartService.selectHeartById(id));
    }

    /**
     * 新增心跳
     */
    @PreAuthorize("@ss.hasPermi('system:heart:add')")
    @Log(title = "心跳", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Heart heart) {
        return toAjax(heartService.insertHeart(heart));
    }

    /**
     * 修改心跳
     */
    @PreAuthorize("@ss.hasPermi('system:heart:edit')")
    @Log(title = "心跳", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Heart heart) {
        return toAjax(heartService.updateHeart(heart));
    }

    /**
     * 删除心跳
     */
    @PreAuthorize("@ss.hasPermi('system:heart:remove')")
    @Log(title = "心跳", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(heartService.deleteHeartByIds(ids));
    }
}
